package com.huang.servlet;

import com.huang.info.UserInfo;
import com.huang.datebase.UserReg;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

@WebServlet(name = "regServlet",urlPatterns = {"/regServlet"})
public class RegServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		java代码写在这里

		System.out.println("----正在执行Servlet，实现方式是 实现：javax.servlet.Servlet----");
		//设置请求的编码格式
		req.setCharacterEncoding("UTF-8");

		//设置相应的编码格式
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html");

		//调用方法
		UserReg userReg = new UserReg();
		UserInfo userInfo = new UserInfo();

		String phone = req.getParameter("phone");
		String password = req.getParameter("password");
		String sex = req.getParameter("sex");
		String[] hobbies = req.getParameterValues("hobby");
		String hobbyStr = Arrays.toString(hobbies);
		String age = req.getParameter("age");
		String header = req.getParameter("header");
		String education = req.getParameter("education");
		String mySelf = req.getParameter("mySelf");

		userInfo.setPhone(phone);
		userInfo.setPassword(password);
		userInfo.setSex(sex);
		userInfo.setHobby(hobbyStr);
		userInfo.setAge(age);
		userInfo.setHeader(header);
		userInfo.setEducation(education);
		userInfo.setMyself(mySelf);


		System.out.println(userInfo);

		boolean reg = userReg.reg(userInfo);

		if (reg == false) {
			PrintWriter pw = resp.getWriter();

			StringBuffer sb = new StringBuffer();

			sb.append("<html>");
			sb.append("<head>");
			sb.append("<title>");
			sb.append("错误页面");
			sb.append("</title>");
			sb.append("</head>");
			sb.append("<body>");
			sb.append("<h3>").append("注册失败").append("</h3>");
			sb.append("</body>");
			sb.append("</html>");
			pw.write(sb.toString());
		} else {
			//跳转到登录页面
			resp.sendRedirect("/pages/login.jsp");
		}
	}
}
